import { Toast } from 'antd-mobile'
import * as _ from 'lodash'
import * as Libs from '../../utils/Libs'
import * as cashflowService from '../../services/home/cashflowService'
import * as assetLiabilityService from '../../services/home/assetLiabilityService'
import { createAction, NavigationActions } from '../../utils'

const initState = {
  cashFlowSummary: {
    jyje: {onRise: 0, showMoney: 0}, //经营活动产生的现金流量净额
    tzje: {onRise: 0, showMoney: 0}, //投资活动产生的现金流量净额
    czje: {onRise: 0, showMoney: 0}, //筹资活动现金流入小计
    xjze: {onRise: 0, showMoney: 0}, //现金及现金等价物净增加额
    qmdjw: {onRise: 0, showMoney: 0}, //期末现金及现金等价物余额
    times: ""
  },
  yj: "",
  gy: "",
  tz: [],
  tzSummary: {},
  jy: [],
  jySummary: {},
  cz: [],
  czSummary: {},
  hlv: {},
  xjjdjw: {},
  qcxianj: {},
  qmxjdjw: {},
  cashMainHistogram: {
    lineDatas: [[0,0,0,0],[0,0,0,0]]
  },
  cashFlowInOutHistogram: {
    lineDatas: [
      [0,0,0], //本期流入  经营，投资，筹资
      [0,0,0], //上期流入
      [0,0,0], //本期流出
      [0,0,0], //上期流出
    ]
  }
}

export default {
  namespace: 'cashflow',
  state: initState,
  reducers: {
    initState(state, { payload }) {
      return { ...initState, ...payload }
    },
    changeState(state, { payload }) {
      return { ...state, ...payload }
    }
  },
  effects: {
    *getSummaryData({payload}, { call, select, put}) {
    },
    *getCashFlowDetailMsg({payload}, { call, select, put}) {
      let params = {}
      params.startTime = (payload.startTime || "").replace('.','-')
      params.endTime = (payload.endTime || "").replace('.','-')
      const resp = yield call(cashflowService.getDetailMsg, params)
      if (resp.resultCode !== 200) {
        Toast.info(resp.msg, 1)
        return false
      }
      const respData = resp.data || []
      const tz = respData.tz || {}
      const tzhdlr = tz.tzhdlr || {}
      const tzhdlrTotal = tzhdlr.total || {}
      const tzhdlrXq = tzhdlr.xq || []
      const tzhdlrRespD = cashflowService.buildD("投资活动流入", tzhdlrTotal, tzhdlrXq)

      const tzhdlc = tz.tzhdlc || {}
      const tzhdlcTotal = tzhdlc.total || {}
      const tzhdlcXq = tzhdlc.xq || []
      const tzhdlcRespD = cashflowService.buildD("投资活动流出", tzhdlcTotal, tzhdlcXq)

      const tzTotal = tz.alltotal[0] || {}

      const cz = respData.cz || {}
      const czhdlr = cz.czhdlr || {}
      const czhdlrTotal = czhdlr.total || {}
      const czhdlrXq = czhdlr.xq || []
      const czhdlrRespD = cashflowService.buildD("筹资活动流入", czhdlrTotal, czhdlrXq)

      const czhdlc = cz.czhdlc || {}
      const czhdlcTotal = czhdlc.total || {}
      const czhdlcXq = czhdlc.xq || []
      const czhdlcRespD = cashflowService.buildD("筹资活动流出", czhdlcTotal, czhdlcXq)

      const jy = respData.jy || {}
      const jyhdlr = jy.jyhdlr || {}
      const jyhdlrTotal = jyhdlr.total || {}
      const jyhdlrXq = jyhdlr.xq || []
      const jyhdlrRespD = cashflowService.buildD("经营活动流入", jyhdlrTotal, jyhdlrXq)

      const jyhdlc = jy.jyhdlc || {}
      const jyhdlcTotal = jyhdlc.total || {}
      const jyhdlcXq = jyhdlc.xq || []
      const jyhdlcRespD = cashflowService.buildD("经营活动流出", jyhdlcTotal, jyhdlcXq)

      let _jy = []
      _jy.push(jyhdlrRespD)
      _jy.push(jyhdlcRespD)

      let _jySummary = cashflowService.buildSummary("经营活动产生的现金流量净额", jy.alltotal[0] || {})

      let _tz = []
      _tz.push(tzhdlrRespD)
      _tz.push(tzhdlcRespD)
      let _tzSummary = cashflowService.buildSummary("投资活动产生的现金流净额", tz.alltotal[0] || {})

      let _cz = []
      _cz.push(czhdlrRespD)
      _cz.push(czhdlcRespD)
      let _czSummary = cashflowService.buildSummary("筹资活动产生的现金流净额", cz.alltotal[0] || {})

      const _hlv = cashflowService.buildSummary("汇率变动对现金及现金等价物的影响", respData.hlv || {})
      const _xjjdjw = cashflowService.buildSummary("现金及现金等价物净增加额", respData.xjjdjw || {})
      const _qcxianj = cashflowService.buildSummary("期初现金及现金等价物余额", respData.qcxianj || {})
      const _qmxjdjw = cashflowService.buildSummary("期末现金及现金等价物余额", respData.qmxjdjw || {})

      yield put(createAction("changeState")({
        jy: _jy,
        jySummary: _jySummary,
        tz: _tz,
        tzSummary: _tzSummary,
        cz: _cz,
        czSummary: _czSummary,
        hlv: _hlv,
        xjjdjw: _xjjdjw,
        qcxianj: _qcxianj,
        qmxjdjw: _qmxjdjw
      }))

    }

    ,
    *getCashFlowMainMsg({payload}, { call, select, put}) {
      let params = {}
      params.startTime = (payload.startTime || "").replace('.','-')
      params.endTime = (payload.endTime || "").replace('.','-')
      const resp = yield call(cashflowService.getMainMsg, params)
      if (resp.resultCode !== 200) {
        Toast.info(resp.msg, 1)
        return false
      }
      const respData = resp.data || {}
      const jyje = respData['jyje'] || {}
      const jyje_SHOWMONEY = Libs.numberUpperFormat(jyje.sumMoney, true)
      const tzje = respData['tzje'] || {}
      const tzje_SHOWMONEY = Libs.numberUpperFormat(tzje.sumMoney, true)
      const czje = respData['czje'] || {}
      const czje_SHOWMONEY = Libs.numberUpperFormat(czje.sumMoney, true)
      const xjze = respData['xjze'] || {}
      const xjze_SHOWMONEY = Libs.numberUpperFormat(xjze.sumMoney, true)
      const qmdjw = respData['qmdjw'] || {}
      const qmdjw_SHOWMONEY = Libs.numberUpperFormat(qmdjw.sumMoney, true)
      yield put(createAction("changeState")({
        cashFlowSummary: {
          jyje: {onRise: jyje.onRise, showMoney: jyje_SHOWMONEY},
          tzje: {onRise: tzje.onRise, showMoney: tzje_SHOWMONEY},
          czje: {onRise: czje.onRise, showMoney: czje_SHOWMONEY},
          xjze: {onRise: xjze.onRise, showMoney: xjze_SHOWMONEY},
          qmdjw: {onRise: qmdjw.onRise, showMoney: qmdjw_SHOWMONEY},
          times: respData.times || ""
        }
      }))
    },
    *getContentMsg({payload}, {call, select, put}) {
      let params = {}
      params.startTime = (payload.startTime || "").replace('.','-')
      params.endTime = (payload.endTime || "").replace('.','-')
      params.type = 3
      const resp = yield call(assetLiabilityService.getShouyeContentMsg, params)
      if (resp.resultCode !== 200) {
        Toast.info(resp.msg, 1)
        return false
      }
      const respData = resp.data || {}
      const yj = (respData.yj || [])[0]
      const gy = (respData.gy || [])[0]
      console.log('yj ', yj, ' gy ', gy)
      yield put(createAction("changeState")({
        yj: yj,
        gy: gy
      }))
    },

    *getCashMainHistogram({payload}, { call, select, put}) {
      let params = {}
      params.startTime = (payload.startTime || "").replace('.','-')
      params.endTime = (payload.endTime || "").replace('.','-')
      params.useArea = "42,43,37,41"
      const resp = yield call(cashflowService.getMainHistogram, params)
      if (resp.resultCode !== 200) {
        Toast.info(resp.msg, 1)
        return false
      }
      const respData = resp.data || []
      const resultCy = respData.resultCy || []

      const jyje = _.find(resultCy, {useArea: '42'}) || {}
      const jyjeCylinder = (jyje.cylinder || [])[0] || {}

      const tzje = _.find(resultCy, {useArea: '43'}) || {}
      const tzjeCylinder = (tzje.cylinder || [])[0] || {}

      const czje = _.find(resultCy, {useArea: '37'}) || {}
      const czjeCylinder = (czje.cylinder || [])[0] || {}

      const qmdjw = _.find(resultCy, {useArea: '41'}) || {}
      const qmdjwCylinder = (qmdjw.cylinder || [])[0] || {}

      const cylinderUp = [jyjeCylinder.tbSumMoney || 0, tzjeCylinder.tbSumMoney || 0, czjeCylinder.tbSumMoney || 0, qmdjwCylinder.tbSumMoney || 0]
      const cylinder = [jyjeCylinder.sumMoney || 0, tzjeCylinder.sumMoney || 0, czjeCylinder.sumMoney || 0, qmdjwCylinder.sumMoney || 0]
      yield put(createAction("changeState")({
        cashMainHistogram: {
          lineDatas: [cylinder, cylinderUp]
        }
      }))
    },
    *getCashFlowInOutHistogram({payload}, {call, select, put}) {
      let params = {}
      params.startTime = (payload.startTime || "").replace('.', '-')
      params.endTime = (payload.endTime || "").replace('.', '-')
      params.useArea = "31,32,33,34,35,36"
      const resp = yield call(cashflowService.getMainHistogram, params)
      if (resp.resultCode !== 200) {
        Toast.info(resp.msg, 1)
        return false
      }
      const respData = resp.data || []
      const resultCy = respData.resultCy || []
      const jyLR = _.find(resultCy, {useArea: '31'}) || {}
      const jyLRCylinder = (jyLR.cylinder || [])[0] || {}

      const jyLC = _.find(resultCy, {useArea: '32'}) || {}
      const jyLCCylinder = (jyLC.cylinder || [])[0] || {}

      const tzLR = _.find(resultCy, {useArea: '33'}) || {}
      const tzLRCylinder = (tzLR.cylinder || [])[0] || {}

      const tzLC = _.find(resultCy, {useArea: '34'}) || {}
      const tzLCCylinder = (tzLC.cylinder || [])[0] || {}

      const czLR = _.find(resultCy, {useArea: '35'}) || {}
      const czLRCylinder = (czLR.cylinder || [])[0] || {}

      const czLC = _.find(resultCy, {useArea: '36'}) || {}
      const czLCCylinder = (czLC.cylinder || [])[0] || {}

      const combineLine = [
        [jyLRCylinder.sumMoney || 0, tzLRCylinder.sumMoney || 0, czLRCylinder.sumMoney || 0],
        [jyLRCylinder.tbSumMoney || 0, tzLRCylinder.tbSumMoney || 0, tzLRCylinder.tbSumMoney || 0],
        [(0 - jyLCCylinder.sumMoney || 0), (0 - tzLCCylinder.sumMoney || 0), (0 - czLCCylinder.sumMoney || 0)],
        [(0 - jyLCCylinder.tbSumMoney || 0), (0 - tzLCCylinder.tbSumMoney || 0), (0 - czLCCylinder.tbSumMoney || 0)],
      ]
      yield put(createAction("changeState")({
        cashFlowInOutHistogram: {
          lineDatas: combineLine
        }
      }))
    }
  }
}
